package com.webblog.controller;

import com.webblog.pojo.Tag;
import com.webblog.service.tag.TagService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Controller
@RequestMapping("/tag")
public class TagController {

    Logger logger = Logger.getLogger(TagController.class);

    @Autowired
    private TagService tagService;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Map addTag(Tag tag){
        Map resultMap = new HashMap();
        Tag resTag = tagService.findAllTagByName(tag.getTagName());
        if(tagService.addTag(tag)>0)
        {
            resultMap.put("success",true);
            resultMap.put("msg","添加标签成功");
            resultMap.put("data",resTag);
        }
        else
        {
            resultMap.put("success",false);
            resultMap.put("msg","添加标签失败");
        }
        return resultMap;
    }

    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Map removeTag(@PathVariable int id){
        logger.info(id);
        Map resultMap = new HashMap();
        if(tagService.removeTag(id)>0)
        {
            resultMap.put("success",true);
            resultMap.put("msg","删除标签成功");
        }
        else
        {
            resultMap.put("success","false");
            resultMap.put("msg","删除标签失败");
        }
        return resultMap;
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Map updateTag(Tag tag){
        Map resultMap = new HashMap();
        if(tagService.updateTag(tag)>0)
        {
            resultMap.put("success",true);
            resultMap.put("msg","修改标签成功");
        }
        else
        {
            resultMap.put("success",false);
            resultMap.put("msg","修改标签失败");
        }
        return resultMap;
    }

    @RequestMapping(value = "/select",method = RequestMethod.GET)
    @ResponseBody
    public Map findAllTag(Tag tag){
        Map resultMap = new HashMap();
        List<Tag> tags = tagService.findAllTag();
        resultMap.put("data",tags);
        return resultMap;
    }

    @RequestMapping(value = "/select/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Map findAllTag(@PathVariable int id){
        Map resultMap = new HashMap();
        Tag tag = tagService.findAllTagById(id);
        resultMap.put("data",tag);
        return resultMap;
    }

}
